import { h, VNode } from 'vue';

export const useComputed = () => {
  const getSummaries = (param: { columns: any[]; data: any[]; count: number; money: number; column1: number; column2: number }) => {
    const { columns, data, count, money, column1, column2 } = param;
    const sums: (string | VNode)[] = new Array(columns.length);
    sums[0] = h('div', { style: { textDecoration: 'underline' } }, ['合计']);

    columns.forEach((column, index) => {
      if (index === column1 || index === column2) {
        const values = data.map((item) => Number(item[column.property]));
        if (!values.every((value) => Number.isNaN(value))) {
          const total = values.reduce((prev, curr) => {
            const value = Number(curr);
            return Number.isNaN(value) ? prev : prev + curr;
          }, 0);
          sums[index] = total.toFixed(2);
          if (index === column1) {
            param.count = total;
          } else if (index === column2) {
            param.money = total;
          }
        } else {
          sums[index] = 'N/A';
        }
      }
    });

    return sums;
  };

  return {
    getSummaries
  };
};
